// 图表库实例化后储存的函数
var widget = null
// 进入页面 默认展示的产品
var index_market = 'btcusdt'
// 进入页面 默认展示的产品周期
var index_activeCycle = '1'

function GetQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if (r != null) return unescape(r[2]); return null;
}
var symbol = GetQueryString("symbol");
if (symbol) {
	index_market = symbol
}
document.title = symbol.toUpperCase()
//console.log(symbol)

if(!localStorage.getItem('lang')){
	localStorage.setItem('lang','zh');
}

// console.log(localStorage.getItem('lang'));


if (localStorage.getItem('lang') == 'hk') {
	document.getElementById('opening').innerText = "開盤";
	document.getElementById('volume').innerText = "成交量";
	document.getElementById('lowest').innerText = "最低";
	document.getElementById('highest').innerText = "最高";
	// document.getElementById('wan').innerText = "萬";
} else if (localStorage.getItem('lang') == 'kr') {
	document.getElementById('opening').innerText = "열리는";
	document.getElementById('volume').innerText = "용량";
	document.getElementById('lowest').innerText = "가장 낮은";
	document.getElementById('highest').innerText = "제일 높은";
	// document.getElementById('wan').innerText = "만";
} else if (localStorage.getItem('lang') == 'jp') {
	document.getElementById('opening').innerText = "オープニング";
	document.getElementById('volume').innerText = "音量";
	document.getElementById('lowest').innerText = "最低";
	document.getElementById('highest').innerText = "最高";
	// document.getElementById('wan').innerText = "万";
} else if (localStorage.getItem('lang') == 'en') {
	document.getElementById('opening').innerText = "opening";
	document.getElementById('volume').innerText = "volume";
	document.getElementById('lowest').innerText = "lowest";
	document.getElementById('highest').innerText = "highest";
	// document.getElementById('wan').innerText = "ten thousand";
} else if (localStorage.getItem('lang') == 'vn') {
	document.getElementById('opening').innerText = "khai mạc";
	document.getElementById('volume').innerText = "âm lượng";
	document.getElementById('lowest').innerText = "thấp nhất";
	document.getElementById('highest').innerText = "Cao nhất";
	// document.getElementById('wan').innerText = "Mười nghìn";
} else if (localStorage.getItem('lang') == 'lta') {
	document.getElementById('opening').innerText = "Early morning";
	document.getElementById('volume').innerText = "Volume";
	document.getElementById('lowest').innerText = "LOW";
	document.getElementById('highest').innerText = "HiG";
	// document.getElementById('wan').innerText = "Mười nghìn";
} else if (localStorage.getItem('lang') == 'es') {
	document.getElementById('opening').innerText = "Temprano en la mañana";
	document.getElementById('volume').innerText = "Volumen";
	document.getElementById('lowest').innerText = "BAJO";
	document.getElementById('highest').innerText = "Alto";
	// document.getElementById('wan').innerText = "Mười nghìn";
}

Event.on('futures.info', data => {
	// 字段		数据类型		描述
	// id		integer		unix时间，同时作为消息ID
	// amount	float		24小时成交量
	// count	integer		24小时成交笔数
	// open		float		24小时开盘价
	// close	float		最新价
	// low		float		24小时最低价
	// high		float		24小时最高价
	// vol		float		24小时成交额
	// console.log(data,'--------------------market-detail')
	/*
	var change = ((data.close - data.open) / data.open * 100).toFixed(2) //亏盈百分比
	document.getElementById('close').innerText = data.close.toFixed(2)
	document.getElementById('cny').innerText = (data.close * change * 0.01).toFixed(2) //亏盈金额
	document.getElementById('change').innerText = (change > 0 ? '+' : '') + change + '%'
	document.getElementById('high').innerText = data.high.toFixed(2)
	document.getElementById('low').innerText = data.low.toFixed(2)
	document.getElementById('amount').innerText = data.amount.toFixed(2)
	// document.getElementById('amount').innerText = (data.amount / 10000).toFixed(2)
	document.getElementById('open').innerText = data.open.toFixed(2)*/
	
	document.getElementById('close').innerText = data.price
	document.getElementById('cny').innerText = data.increase
	document.getElementById('change').innerText = (data.ratio > 0 ? '+' : '') + data.ratio + '%'
	document.getElementById('high').innerText = data.high
	document.getElementById('low').innerText = data.low
	document.getElementById('amount').innerText = data.amount
	// document.getElementById('amount').innerText = (data.amount / 10000)
	document.getElementById('open').innerText = data.open
	
	
	var color = ''
	if (data.ratio > 0) {
		color = "#1cad90"
	}
	if (data.ratio < 0) {
		color = "#ff6059"
	}
	document.getElementById('close').style.color = color
	document.getElementById('change').style.color = color
	document.getElementById('cny').style.color = color

})
// console.log(window.TradingView)
// window.TradingView.onready 确保在html的dom加载完成后在调用

function initOnReady() {
	// chartConfig 在chartConfig.js里面
	// 给chartConfig添加展示周期
	chartConfig.interval = index_activeCycle
	// 给chartConfig添加展示产品
	chartConfig.symbol = index_market

	// 初始化 TradingView
	widget = new window.TradingView.widget(chartConfig)
	//console.log("widget", widget)
	widget && widget.onChartReady && widget.onChartReady(function () {
		// 1 为平均K线； 3 为面积图
		/*widget.chart().setChartType(index_activeCycle == '2' ? 3 : 1)
		
		widget.chart().createStudy('Moving Average', !1, !0, [10, 'close', 10], null, {
			'Plot.color': '#e843da'
		})*/
		
		// 这是k线图 展示的 7日均线和30日均线。
		widget.chart().createStudy('Moving Average', false, false, [7], null, { 'Plot.linewidth': 2, 'Plot.color': '#2ba7d6' })
		widget.chart().createStudy('Moving Average', false, false, [30], null, { 'Plot.linewidth': 2, 'Plot.color': '#de9f66' })
		setTimeout(() => {
			widget.chart().resetData()
		}, 1000)
	})

//	init();
}

window.addEventListener('DOMContentLoaded', initOnReady, false);

var intervalDom = document.getElementById('interval')

// 切换产品周期
intervalDom.addEventListener('click', function (e) {
	// e.target.dataset.value 这个就是获取的产品的周期
	// console.log(e.target.dataset.value)
	// 1 为平均K线； 3 为面积图
	widget.chart().setChartType(e.target.dataset.value == '2' ? 3 : 1)
	widget.chart().setResolution(e.target.dataset.value)
	// 这个函数不用看，我为了样式好看 写一个添加删除class
	addClass(intervalDom, e.target)
}, false)


// 单纯的写一个添加class的函数，这个不用看 没用
function addClass(fatherDom, dom) {
	[...fatherDom.getElementsByTagName('span')].forEach(function (item) {
		item.className = ''
	})
	dom.className = 'active'
}
